<template>
  <div class="mod-config">
    <el-table
      :span-method="objectSpanMethod"
      size="mini"
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        prop="dName"
        header-align="center"
        align="center"
        label="设备"
      width="200">
      </el-table-column>
      <el-table-column
        prop="tName"
        header-align="center"
        align="center"
        label="节目">
      </el-table-column>
      <el-table-column
        prop="status"
        header-align="center"
        align="center"
        label="节目状态"
         width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 1" type="success" size="mini">正常</el-tag>
          <el-tag v-if="scope.row.status === 0" size="mini" >下线</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="synchronize"
        header-align="center"
        align="center"
        label="同步进度" >
        <template slot-scope="scope">
          <el-progress :text-inside="true" :stroke-width="20" status="success" :percentage="scope.row.synchronize"></el-progress>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
  </div>
</template>
<style>
   .cell{
    display: inline-block;
  }
</style>
<script>
  export default {
    name: 'publish-device-theme',
    data () {
      return {
        dataForm: {
          pid: null
        },
        timer: null,
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    mounted(){
      this.timer = setInterval(() => {
        this.getDataList()
      }, 1000)
    },
    destroyed: function () {
      clearInterval(this.timer);
    },
    methods: {
      init (pid) {
        this.dataForm.pid = pid
        this.getDataList()
      },
      // 获取数据列表
      getDataList () {
        console.log('run in getData')
        // this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/sys/publishdevicetheme/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'pid': this.dataForm.pid
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          // this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      objectSpanMethod ({ row, column, rowIndex, columnIndex }) {
        if (columnIndex === 0) {
          if (rowIndex !== 0 && this.dataList[rowIndex - 1].did === row.did) {
            return {
              rowspan: 0,
              colspan: 0
            }
          } else {
            return {
              rowspan: this.checkCount(row.did),
              colspan: 1
            }
          }
        }
      },
      checkCount (did) {
        let count = 0
        for (let i = 0; i < this.dataList.length; i++) {
          if (did === this.dataList[i].did) {
            count++
          }
        }
        return count
      }
    }
  }
</script>
